package com.ubxtech.cloud.config;

import cn.hutool.core.util.StrUtil;
import com.ubxtech.cloud.client.MsgPushClient;
import com.ubxtech.cloud.client.MsgWebsocketServiceIdClient;
import com.ubxtech.cloud.client.PushPathClient;
import com.ubxtech.cloud.properties.CloudProperties;
import feign.Feign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author Victor.Xiao
 * @since 2023-11-23 20:48
 **/

@Slf4j
@Configuration
@EnableConfigurationProperties(CloudProperties.class)
@ConditionalOnClass(Feign.class)
@AutoConfigureAfter(EnableFeignClients.class)
public class CloudConfig {
    /**
     * 用于动态构造FeignClient
     * @author Victor.Xiao
     * @since 2023-11-23 19:35
     * @param applicationContext applicationContext
     * @return org.springframework.cloud.openfeign.FeignClientBuilder
     */
    @Bean
    @ConditionalOnMissingBean
    public FeignClientBuilder feignClientBuilder(ApplicationContext applicationContext) {
        return new FeignClientBuilder(applicationContext);
    }

    @Bean
    @ConditionalOnMissingBean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    @ConditionalOnMissingBean
    public MsgPushClient msgPushClient(FeignClientBuilder feignClientBuilder,CloudProperties cloudProperties) {
        if (StrUtil.isBlank(cloudProperties.getMsgPushServiceUrl())) {
            return null;
        }
        return feignClientBuilder.forType(MsgPushClient.class,cloudProperties.getMsgServiceName()).path(cloudProperties.getMsgPushServiceUrl()).contextId("MsgPushClient").build();
    }

    @Bean
    @ConditionalOnMissingBean
    public PushPathClient pushPathClient(FeignClientBuilder feignClientBuilder, CloudProperties cloudProperties) {
        if (StrUtil.isBlank(cloudProperties.getPushPathUrl())) {
            return null;
        }
        FeignClientBuilder.Builder<PushPathClient> builder = feignClientBuilder.forType(PushPathClient.class,cloudProperties.getMsgServiceName()).contextId("PushPathClient").path(cloudProperties.getPushPathUrl());
        if (StrUtil.isNotBlank(cloudProperties.getMsgServiceBaseUrl())) {
            builder.url(cloudProperties.getMsgServiceBaseUrl());
        }

        return builder.build();
    }

    @Bean
    @ConditionalOnMissingBean
    public MsgWebsocketServiceIdClient msgWebsocketServiceIdClient(FeignClientBuilder feignClientBuilder, CloudProperties cloudProperties) {
        if (StrUtil.isBlank(cloudProperties.getMsgGetServiceIdUrl())) {
            return null;
        }
        return feignClientBuilder.forType(MsgWebsocketServiceIdClient.class,cloudProperties.getMsgServiceName()).path(cloudProperties.getMsgGetServiceIdUrl()).contextId("MsgWebsocketServiceIdClient").build();
    }
}
